package exercicio01;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/tabuada")
public class TabuadaServlet extends HttpServlet {

	private static final long serialVersionUID = 2165160604915514115L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		processar(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		processar(request, response);
	}	
	
	public void processar(HttpServletRequest request, HttpServletResponse response) {
		PrintWriter out;
		
		try {
			
			out = response.getWriter();
			response.setContentType("text/html");
			
			out.println("<html><body>");
			out.println("<h1>Tabuada</h1>");
			
			if (request.getParameter("numero") != null) {
				
				Integer numero = Integer.parseInt(request.getParameter("numero"));
				
				out.println("<table>");
				
				for (int i = 0; i < 11; i++) {
				
					out.println("<tr>");
					out.println("  <td> " + numero + " </td>");
					out.println("  <td> X </td>");
					out.println("  <td> " + i + " </td>");
					out.println("  <td> = " + numero * i + "</td>");
					out.println("</tr>");
				
				}
				
				out.println("</table>");
			} else {
				out.println("<h3>Informe um número como parâmetro!</h3>");
			}			
			
			out.println("</body></html>");
			out.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
}
